package com.mark.controller.protal;

import com.mark.common.ServerResponse;
import com.mark.entity.ProductCategory;
import com.mark.entity.ProductInfo;
import com.mark.service.IProductCategoryService;
import com.mark.service.IProductInfoService;
import com.mark.vo.ProductInfoVo;
import com.mark.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Choisaaaa on 2018/6/21.
 * 买家商品
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private IProductInfoService iProductInfoService;
    @Autowired
    private IProductCategoryService iProductCategoryService;


    @GetMapping("list")
    public ServerResponse<List<ProductVo>> list(){
        //1.查询所有上架的商品
        List<ProductInfo> productInfoList = iProductInfoService.findUpAll();

        //2.查询商品类目
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());
        List<ProductCategory> productCategoryList = iProductCategoryService.findByCategoryTypeIn(categoryTypeList);

        //3.数据拼装
        List<ProductVo> productVoList = new ArrayList<>();
        for(ProductCategory productCategory : productCategoryList){
            ProductVo productVo = new ProductVo();
            productVo.setCategoryName(productCategory.getCategoryName());
            productVo.setCategoryType(productCategory.getCategoryType());
            List<ProductInfoVo> productInfoVoList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList){//遍历商品
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {//根据商品类目筛选商品
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo,productInfoVo);
                    productInfoVoList.add(productInfoVo);
                }
            }
            productVo.setProductInfoVoList(productInfoVoList);
            productVoList.add(productVo);
        }

        return ServerResponse.createBySuccess(productVoList);
    }
}
